package com.sunchangpeng.wechat.mp.template;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.sunchangpeng.wechat.core.WechatClient;
import com.sunchangpeng.wechat.mp.EndpointProvider;
import com.sunchangpeng.wechat.mp.MpAppSetting;
import com.sunchangpeng.wechat.mp.MpWechatClientFactory;
import lombok.Data;

import java.util.List;
import java.util.Map;

public class Templates {
    private WechatClient wechatClient;

    public void setWechatClient(WechatClient wechatClient) {
        this.wechatClient = wechatClient;
    }

    public static Templates with(MpAppSetting appSetting) {
        Templates templates = new Templates();
        templates.setWechatClient(MpWechatClientFactory.getInstance().with(appSetting));
        return templates;
    }

    /**
     * 设置所属行业
     */
    public void setIndustries(String primary, String secondary) {
        String url = EndpointProvider.get("url.template.industry.set");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject()
                .fluentPut("industry_id1", primary)
                .fluentPut("industry_id2", secondary)));
    }

    /**
     * 获取所属行业
     */
    public IndustryWrapper getIndustries() {
        String url = EndpointProvider.get("url.template.industry.get");
        String response = wechatClient.getTextWithToken(url);
        return JSON.parseObject(response, IndustryWrapper.class);
    }


    public String addTemplate(String code) {
        String url = EndpointProvider.get("url.template.get");
        String response = wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject().fluentPut("template_id_short", code)));
        return JSON.parseObject(response).getString("template_id");
    }

    /**
     * 获取所有的模板
     */
    public List<Template> list() {
        String url = EndpointProvider.get("url.template.list");
        String response = wechatClient.getTextWithToken(url);
        return JSON.parseObject(response, TemplateListWrapper.class).getTemplateList();
    }

    /**
     * 删除模板
     */
    public void delete(String templateId) {
        String url = EndpointProvider.get("url.template.delete");
        wechatClient.getTextWithToken(url, JSON.toJSONString(new JSONObject().fluentPut("template_id", templateId)));
    }


    /**
     * 发送模板消息
     */
    public long send(String toUser, String templateId, String url, Map<String, Value> messages) {
        return send(toUser, templateId, url, null, messages);
    }

    /**
     * 发送模板消息
     */
    public long send(String toUser, String templateId, MiniProgram miniProgram, Map<String, Value> messages) {
        return send(toUser, templateId, null, miniProgram, messages);
    }

    /**
     * 发送模板消息
     */
    public long send(String toUser, String templateId, String url, MiniProgram miniProgram, Map<String, Value> messages) {
        String sendUrl = EndpointProvider.get("url.template.send");
        MessageWrapper messageWrapper = new MessageWrapper();
        messageWrapper.setToUser(toUser);
        messageWrapper.setTemplateId(templateId);
        messageWrapper.setData(messages);

        if (url != null) {
            messageWrapper.setUrl(url);
        }

        if (miniProgram != null) {
            messageWrapper.setMiniProgram(miniProgram);
        }

        String json = JSON.toJSONString(messageWrapper);
        String response = wechatClient.getTextWithToken(sendUrl, json);
        return JSON.parseObject(response).getLong("msgid");
    }


    @Data
    public static class IndustryWrapper {
        @JSONField(name = "primary_industry")
        private Industry primary;

        @JSONField(name = "secondary_industry")
        private Industry secondary;
    }

    @Data
    public static class Industry {
        @JSONField(name = "first_class")
        private String first;

        @JSONField(name = "second_class")
        private String second;
    }

    @Data
    public static class Template {
        @JSONField(name = "template_id")
        private String templateId;
        private String title;

        @JSONField(name = "primary_industry")
        private String primaryIndustry;

        @JSONField(name = "deputy_industry")
        private String secondaryIndustry;

        private String content;
        private String example;
    }

    @Data
    public static class TemplateListWrapper {
        @JSONField(name = "template_list")
        private List<Template> templateList;
    }

    @Data
    public static class Value {
        private String value;
        private String color;

        public Value() {
        }

        public Value(String value, String color) {
            this.value = value;
            this.color = color;
        }
    }

    @Data
    public static class MiniProgram {
        @JSONField(name = "appid")
        private String appId;

        @JSONField(name = "pagepath")
        private String pagePath;
    }


    @Data
    public static class MessageWrapper {
        @JSONField(name = "touser")
        private String toUser;
        @JSONField(name = "template_id")
        private String templateId;
        private String url;
        @JSONField(name = "miniprogram")
        private MiniProgram miniProgram;
        private Map<String, Value> data;
    }
}
